//
//  LYCGetVerificationCodeButton.m
//  DeepBreathing
//
//  Created by Eason on 15/12/18.
//  Copyright © 2015年 Eason Leo. All rights reserved.
//

#import "LYCGetVerificationCodeButton.h"
#define WIDTH(X) X.frame.size.width
#define HEIGHT(Y) Y.frame.size.height
#define NEXT_REQUEST_TIMEINTERVAL 30;

@interface LYCGetVerificationCodeButton ()

@property (strong, nonatomic) UIView *animationView;

@property (strong, nonatomic) UIView *backgroundImageView;

@property (strong, nonatomic) UITapGestureRecognizer *tapGesture;

@property (nonatomic, getter=isBufferAnimation) BOOL bufferAnimation;

@end

@implementation LYCGetVerificationCodeButton

#pragma mark - initialize

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.frame = frame;
        [self initializeButtonInterface];
    }
    return self;
}

#pragma mark - private methods

- (void)initializeButtonInterface {
    [self addSubview:self.backgroundImageView];
    [self addSubview:self.animationView];
    UILabel *titleLabel = ({
        UILabel *label = [[UILabel alloc] initWithFrame:self.bounds];
        label.text = @"获取验证码";
        label.textColor = [UIColor whiteColor];
        label.textAlignment = NSTextAlignmentCenter;
        label.font = [UIFont systemFontOfSize:17];
        label;
    });
    [self addSubview:titleLabel];
    [self addGestureRecognizer:self.tapGesture];
}
- (CAAnimation *)setBufferAnimationInView:(UIView *)view {
    CABasicAnimation *basicAnimation = [CABasicAnimation animationWithKeyPath:@"bounds.size.width"];
    basicAnimation.duration = NEXT_REQUEST_TIMEINTERVAL;
    basicAnimation.fromValue = [NSNumber numberWithInteger:WIDTH(view)];
    basicAnimation.toValue = [NSNumber numberWithInteger:WIDTH(self)];
    CABasicAnimation *basicAnimationPosition = [CABasicAnimation animationWithKeyPath:@"position"];
    basicAnimationPosition.duration = NEXT_REQUEST_TIMEINTERVAL;
    basicAnimationPosition.fromValue = [NSValue valueWithCGPoint:CGPointMake(0, HEIGHT(self) / 2)];
    basicAnimationPosition.toValue = [NSValue valueWithCGPoint:CGPointMake(WIDTH(self) / 2, HEIGHT(self) / 2)];
    CAAnimationGroup *animationGroup = [CAAnimationGroup animation];
    animationGroup.delegate = self;
    animationGroup.duration = NEXT_REQUEST_TIMEINTERVAL;
    animationGroup.animations = @[basicAnimationPosition, basicAnimation];
    return animationGroup;
}



#pragma mark - callback/action

- (void)getVerificationCodeButtonTaped:(UITapGestureRecognizer *)gesture {
    if (gesture.state == UIGestureRecognizerStateEnded) {
        if (self.isBufferAnimation) {
            return;
        }
        self.backgroundImageView.backgroundColor = [UIColor lightGrayColor];
        [self.animationView.layer addAnimation:[self setBufferAnimationInView:self.animationView] forKey:@"getVerificationCodeBufferAnimation"];
        self.bufferAnimation = YES;
        
        if (_delegate && [_delegate respondsToSelector:@selector(doNetAction)]) {
            [_delegate doNetAction];
        }
        
        
    }
}

#pragma mark - AnimationDelegate

- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag {
    if (flag) {
        self.backgroundImageView.backgroundColor = [UIColor orangeColor];
        self.bufferAnimation = NO;
    }
}

#pragma mark - getter

- (UIView *)animationView {
    if (!_animationView) {
        _animationView = ({
            CGRect customFrame = self.bounds;
            customFrame.size.width = 0;
            UIView *view = [[UIView alloc] initWithFrame:customFrame];
            view.backgroundColor = [UIColor orangeColor];
            view;
        });
    }
    return _animationView;
}
- (UIView *)backgroundImageView {
    if (!_backgroundImageView) {
        _backgroundImageView = ({
                UIView *view = [[UIView alloc] initWithFrame:self.bounds];
                view.backgroundColor = [UIColor orangeColor];
                view;
        });
    }
    return _backgroundImageView;
}
- (UITapGestureRecognizer *)tapGesture {
    if (!_tapGesture) {
        _tapGesture = ({
            UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(getVerificationCodeButtonTaped:)];
            tap;
        });
    }
    return _tapGesture;
}

@end
